package com.example.demo03

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import kotlin.div
import kotlin.inc
import kotlin.math.min
import kotlin.rem

class FourthlyActivity : AppCompatActivity() {
    private var seconds = 0
    private var running = false
    private var wasRunning = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_fourthly)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }



        fun runTimer() {
            val handler = Handler();
            val runnable = object : Runnable {
                override fun run() {
                    val hours = seconds / 3600
                    val minutes = (seconds % 3600) / 60
                    val secs = seconds % 60
                    findViewById<TextView>(R.id.textView_timer).text =
                        String.format("%02d:%02d:%02d", hours, minutes, secs)
                    if (running) {
                        seconds++
                    }
                    handler.postDelayed(this, 1000)
                }
            }
            handler.post(runnable)
        }
        Log.d("Life cycle", "onCreate")
        // 2. 从 savedInstanceState 恢复状态（如果存在）
        if (savedInstanceState != null) {
            seconds = savedInstanceState.getInt("seconds")
            running = savedInstanceState.getBoolean("running")
            wasRunning = savedInstanceState.getBoolean("wasRunning")
        }

        runTimer()

        findViewById<Button>(R.id.button_start).setOnClickListener {
            running = true;
        }
        findViewById<Button>(R.id.button_stop).setOnClickListener {
            running = false
        }
        findViewById<Button>(R.id.button_restart).setOnClickListener {
            running = true
            seconds = 0
        }


    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("Life cycle", "onSaveInstanceState")
        outState.putInt("seconds", seconds)
        outState.putBoolean("running", running)
        outState.putBoolean("wasRunning", wasRunning)
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("Life cycle", "onDestroy")
    }

    override fun onStart() {
        super.onStart()
        Log.d("Life cycle", "onStart")
    }

    override fun onStop() {
        super.onStop()
        Log.d("life cycle", "onStop")
    }

    override fun onResume() {
        super.onResume()
        Log.d("Life cycle", "onResume")
        if (wasRunning) {
            running = true
        }
    }

    override fun onPause(){
    super.onPause()
    Log.d(  "Life cycle", "onPause")
    wasRunning = running
    running = false
}

}
